package com.viphuli.app.tool.adapter;

import java.util.List;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.ButterKnife;

import com.offroader.utils.ViewUtils;
import com.viphuli.app.tool.R;
import com.viphuli.app.tool.bean.part.AccountUser;
import com.viphuli.app.tool.common.MyApp;
import com.viphuli.app.tool.utils.ImageUtils;

public class ArrangeMemberAdapter extends BaseAdapter {

	private List<AccountUser> list;
	private int selected = -1;

	public ArrangeMemberAdapter(List<AccountUser> list) {
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int index) {
		return list.get(index);
	}

	@Override
	public long getItemId(int index) {
		return index;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;

		if (convertView == null || convertView.getTag() == null) {
			convertView = ViewUtils.inflateView(MyApp.getInstance(), R.layout.frag_arrange_member_item);
			holder = new ViewHolder(convertView);
			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		AccountUser user = list.get(position);

		holder.name.setText(user.getUserName());
		ImageUtils.load(holder.portrait, user.getPortrait());

		if (selected == position) {
			holder.check.setBackgroundResource(R.drawable.ic_arrange_type_selected);
		} else {
			holder.check.setBackgroundResource(R.drawable.ic_arrange_type_un_selected);
		}

		return convertView;
	}

	public void check(int selected) {

		this.selected = selected;

		notifyDataSetChanged();
	}

	public int getCheck() {
		return selected;
	}

	protected class ViewHolder {

		@Bind(R.id.id_circle_member_girdview_item_portrait)
		ImageView portrait;

		@Bind(R.id.id_circle_member_girdview_item_name)
		TextView name;

		@Bind(R.id.id_home_item_arrange_type_check)
		View check;

		public ViewHolder(View view) {
			ButterKnife.bind(this, view);
		}
	}

}
